package com.doris.lucene;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;

/**
 * Tika解析文档，可多线程处理
 * 
 * @author root
 *
 */
public class DocumentParseThread implements Runnable {

	private static final Logger LOGGER = Logger.getLogger(DocumentParseThread.class);
	private static Tika tika = new Tika();
	/**
	 * 文档路径
	 */
	private String path;

	public DocumentParseThread(String path) {
		this.path = path;
	}

	@Override
	public void run() {
		if (path.contains("~$")) { // 编辑中的word文档
			LuceneIndexer.getCounts().incrementAndGet();
			return;
		}
		String content = null;
		File file = new File(path);
		try {
			content = tika.parseToString(file);
			LuceneDocument document = new LuceneDocument(file.getName(), path, content);
			LuceneIndexer.getQueues().add(document);
		} catch (IOException | TikaException e) {
			LOGGER.error("文档解析失败", e);
		} finally {
			LuceneIndexer.getCounts().incrementAndGet();
			file = null;
			LOGGER.debug("parse file " + path);
		}
	}

}
